import os
import sys
import json
import logging
import pymysql
from db_utils import db_utils
import requests
import time
from datetime import datetime, date
from decimal import Decimal
import schedule
from datetime import timedelta


class DataExtractor:

    def __init__(self, table_name, config_file='config.properties'):
        self.config_file = config_file
        self.table_name = table_name
        self.date_format = "%Y-%m-%d"
        self.config = self.load_config()

    def load_config(self):
        """
        加载配置文件（config.properties）
        配置文件包含：
        1. 数据库连接信息：db.host, db.port, db.user, db.password, db.name
        2. 金蝶账套信息：kingdee.acctID, kingdee.username, kingdee.password, kingdee.url
        3. 金蝶应用信息：kingdee.appID, kingdee.appSec

        返回:
            dict: 配置项字典
        """
        config_path = self.config_file
        if not os.path.exists(config_path):
            raise FileNotFoundError("配置文件未找到")

        config = {}
        try:
            with open(config_path, 'r', encoding='utf-8') as f:
                for line in f:
                    line = line.strip()
                    # 跳过注释行和空行
                    if line and not line.startswith('#') and '=' in line:
                        key, value = line.split('=', 1)
                        config[key.strip()] = value.strip()
        except Exception as e:
            logging.error(f"加载配置文件失败: {e}")
            raise
        return config

    # 获取任务队列
    def get_pending_tasks(self):
        conn = None
        cursor = None
        try:
            # 获取数据库连接
            conn, cursor = db_utils.get_db_connection('lqetl')

            task_sql = """
                    SELECT id, tablename,operate_type,starttime,endtime
                    FROM kd_task 
                    WHERE run_flag = 20 
                    ORDER BY id ASC 
                """
            cursor.execute(task_sql)
            tasks = cursor.fetchall()
            logging.info(f"成功获取 {len(tasks)} 个待执行任务（run_flag=20）")
            return tasks
        except Exception as e:
            logging.error(f"获取待执行任务失败: {str(e)}")
            raise
        finally:
            if cursor:
                cursor.close()
            if conn:
                conn.close()

    # 从数据库中抽取任务中指定的数据：按task_id=任务ID筛选
    def extract_data(self, task):
        conn = None
        cursor = None
        try:
            task_id = task['id']
            self.table_name = task['tablename']

            logging.info(
                f"开始执行任务ID {task_id}: "
                f"表名={self.table_name}，筛选条件：task_id = {task_id}"
            )

            # 2. 连接业务数据库，执行数据抽取
            conn, cursor = db_utils.get_db_connection(self.config.get('db.name'))

            # 筛选条件 task_id = 任务ID
            conditions = ["task_id = %s"]
            params = [task_id]

            # 执行数据查询
            sql = f"""
                    SELECT * FROM {self.table_name}
                    WHERE {' AND '.join(conditions)}
                    ORDER BY FBillNo  -- 按单据号排序，便于后续分组
                """
            cursor.execute(sql, params)
            extracted_data = cursor.fetchall()
            logging.info(f"任务ID {task_id} - 成功抽取 {len(extracted_data)} 条数据")
            return extracted_data

        except Exception as e:
            logging.error(f"任务ID {task_id} - 数据抽取失败: {str(e)}")
            raise
        finally:
            if cursor:
                cursor.close()
            if conn:
                conn.close()

    # 更新kd_task表中run_flag字段
    def update_task_run_flag(self, task_id, new_run_flag):
        conn = None
        cursor = None
        try:
            # 获取数据库连接
            conn, cursor = db_utils.get_db_connection('lqetl')
            # 任务状态（21/22/23）：更新run_flag字段
            sql = """
                      UPDATE kd_task 
                      SET run_flag = %s 
                      WHERE id = %s
                 """
            # 执行参数化查询
            cursor.execute(sql, (new_run_flag, task_id))

            # 检查是否有记录被更新
            if cursor.rowcount == 0:
                logging.warning(f"任务ID: {task_id} 不存在或未发生变更，状态更新失败")
                return False

            logging.info(f"任务ID: {task_id} 状态已更新为 {new_run_flag}")
            return True
        except Exception as e:
            logging.error(f"更新任务ID: {task_id} 状态失败: {str(e)}", exc_info=True)
            return False
        finally:
            if cursor:
                cursor.close()
            if conn:
                conn.close()

    # 按单据号（FBillNo）分组数据
    def group_data_by_bill_no(self, extracted_data, task_id):
        """
        任务ID日志
        """
        if not isinstance(extracted_data, list) or len(extracted_data) == 0:
            logging.warning(f"任务ID {task_id} - 输入数据为空或非列表，无需分组")
            return {}

        bill_groups = {}
        for record in extracted_data:
            bill_no = record.get("FBillNo")
            if bill_no not in bill_groups:
                bill_groups[bill_no] = []
            bill_groups[bill_no].append(record)

        logging.info(f"任务ID {task_id} - 数据分组完成，共得到 {len(bill_groups)} 个单据组")
        return bill_groups

    # 数据转换成json格式
    def transform_data(self, bill_data, bill_no, task_id):
        if not bill_data:
            raise ValueError(f"任务ID {task_id} - 单据 {bill_no} 数据为空，无法转换")

        logging.info(f"任务ID {task_id} - 开始转换单据 {bill_no}（含 {len(bill_data)} 条明细）")
        # 金蝶API格式转换逻辑
        first_row = bill_data[0]
        # 蛋疼做法 发现应付单的明细字段名称是：FEntityDetail，而不是跟其他单据一样FEntity
        if getattr(self, 'table_name', '') == "kd_ship_fee":
            bill_model = {
                "NeedUpDateFields": [],
                "NeedReturnFields": ["FID"],
                "IsDeleteEntry": "false",
                "SubSystemId": "",
                "IsVerifyBaseDataField": "false",
                "IsEntryBatchFill": "true",
                "ValidateFlag": "false",
                "NumberSearch": "true",
                "IsAutoAdjustField": "false",
                "InterationFlags": "",
                "IgnoreInterationFlag": "",
                "IsControlPrecision": "false",
                "ValidateRepeatJson": "false",
                # 顶层配置字段
                "Model": {
                    "FBillNo": bill_no,
                    "FMultiEntryFlag": "true",
                    "FEntityDetail": []
                }
            }
        else:
            bill_model = {
                "NeedUpDateFields": [],
                "NeedReturnFields": ["FID"],
                "IsDeleteEntry": "false",
                "SubSystemId": "",
                "IsVerifyBaseDataField": "false",
                "IsEntryBatchFill": "true",
                "ValidateFlag": "false",
                "NumberSearch": "true",
                "IsAutoAdjustField": "false",
                "InterationFlags": "",
                "IgnoreInterationFlag": "",
                "IsControlPrecision": "false",
                "ValidateRepeatJson": "false",
                # 顶层配置字段
                "Model": {
                    "FBillNo": bill_no,
                    "FMultiEntryFlag": "true",
                    "FEntity": []
                }
            }
        # 表单数据包，JSON类型（必录）
        model = bill_model["Model"]
        self._new_map_header_fields_api_style(model, first_row)

        # 映射明细行
        for index, row in enumerate(bill_data):
            entity_item = {"FSeq": index + 1}
            self._new_map_entity_fields_api_style(entity_item, row)
            if getattr(self, 'table_name', '') == "kd_ship_fee":
                model["FEntityDetail"].append(entity_item)
            else:
                model["FEntity"].append(entity_item)
        if getattr(self, 'table_name', '') == "kd_ship_fee":
            logging.info(f"任务ID {task_id} - 单据 {bill_no} 转换完成（生成 {len(model['FEntityDetail'])} 条明细）")
        else:
            logging.info(f"任务ID {task_id} - 单据 {bill_no} 转换完成（生成 {len(model['FEntity'])} 条明细）")
        return bill_model

    # 映射单据主表字段 model 【新】
    def _new_map_header_fields_api_style(self, model, row):
        """
        model[""] = row.get("", "")
        model[""] = {"FUserID": row.get("", "")}
        model[""] = {"FNumber": row.get("", "")}
        model[""] = {"FNUMBER": row.get("", "")}
        """
        # 订单明细表（金蝶测试251002）
        if  getattr(self, 'table_name', '') == 'kd_order_detail':
            # 实体主键
            # model["FID"] = self.replace_none_numb(row.get("FID",0))
            # 单据编号，已统一映射
            # model["FBillNo"] = self.replace_none_str(row.get("",""))
            # 创建人
            #model["F_UNW_CreatorId"] = {"FUserID": self.replace_none_str(row.get("F_UNW_CreatorId", ""))}
            # 创建日期
            # model["F_UNW_CreateDate"] = self.replace_none_date(str(row.get("F_UNW_CreateDate","1900-01-01")))
            # 修改人
            # model["F_UNW_ModifierId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_ModifierId",""))}
            # 修改日期
            # model["F_UNW_ModifyDate"] = self.replace_none_date(str(row.get("F_UNW_ModifyDate","1900-01-01")))
            # 审核人
            # model["F_UNW_APPROVEUserId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_APPROVEUserId",""))}
            # 审核日期
            # model["F_UNW_APPROVEDate"] = self.replace_none_date(str(row.get("F_UNW_APPROVEDate","1900-01-01")))
            # 创建组织 (必填项)
            model["F_UNW_OrgId"] = {"FNumber": self.replace_none_str(row.get("F_UNW_OrgId", ""))}
            # 支付日期
            model["F_UNW_DATE"] = self.replace_none_date(str(row.get("F_UNW_DATE", "1900-01-01")))
            # 【底】店铺名称
            model["F_UNW_DPMC"] = self.replace_none_str(row.get("F_UNW_DPMC", ""))
            # 店铺ID
            model["F_UNW_DPID1"] = self.replace_none_str(row.get("F_UNW_DPID", ""))
            # 销售主体 (必填项)
            model["F_UNW_XSZT"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_XSZT", ""))}
            # 平台编码
            model["F_UNW_PT1"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT1", ""))}
            # 是否同步税务账
            # model["F_XYS_ISSWZ"] = row.get("F_XYS_ISSWZ","false")

        # 退货明细表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_order_return':
            # 实体主键
            #model["FID"] = self.replace_none_numb(row.get("FID",0))
            # 单据编号，已统一映射
            #model["FBillNo"] = self.replace_none_str(row.get("",""))
            # 创建人
            #model["F_UNW_CreatorId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_CreatorId",""))}
            # 创建日期
            #model["F_UNW_CreateDate"] = self.replace_none_date(str(row.get("F_UNW_CreateDate","1900-01-01")))
            # 修改人
            #model["F_UNW_ModifierId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_ModifierId",""))}
            # 修改日期
            #model["F_UNW_ModifyDate"] = self.replace_none_date(str(row.get("F_UNW_ModifyDate","1900-01-01")))
            # 审核人
            #model["F_UNW_APPROVEUserId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_APPROVEUserId",""))}
            # 审核日期
            #model["F_UNW_APPROVEDate"] = self.replace_none_date(str(row.get("F_UNW_APPROVEDate","1900-01-01")))
            # 创建组织 (必填项)
            model["F_UNW_OrgId"] = {"FNumber": self.replace_none_str(row.get("F_UNW_OrgId",""))}
            # 本次发生日期
            model["F_UNW_DATE"] = self.replace_none_date(str(row.get("F_UNW_DATE","1900-01-01")))
            # 业务场景 (必填项)
            model["F_UNW_YWCJ"] = self.replace_none_str(row.get("F_UNW_YWCJ",""))
            # 店铺ID
            model["F_UNW_DPID1"] = self.replace_none_str(row.get("F_UNW_DPID",""))
            # 【底】店铺名称
            model["F_UNW_DPMC"] = self.replace_none_str(row.get("F_UNW_DPMC",""))
            # 销售主体 (必填项)
            model["F_UNW_XSZT"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_XSZT",""))}
            # 平台编码
            model["F_UNW_PT1"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT1",""))}
            # 是否同步税务账
            #model["F_XYS_ISSWZ"] = row.get("F_XYS_ISSWZ","false")

        # 动账明细表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_dzmx':
            # 实体主键
            # model["FID"] = self.replace_none_numb(row.get("FID",0))
            # 单据编号，已统一映射
            # model["FBillNo"] = self.replace_none_str(row.get("",""))
            # 创建人
            #model["F_UNW_CreatorId"] = {"FUserID": self.replace_none_str(row.get("F_UNW_CreatorId", ""))}
            # 创建日期
            # model["F_UNW_CreateDate"] = self.replace_none_date(str(row.get("F_UNW_CreateDate","1900-01-01")))
            # 修改人
            # model["F_UNW_ModifierId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_ModifierId",""))}
            # 修改日期
            # model["F_UNW_ModifyDate"] = self.replace_none_date(str(row.get("F_UNW_ModifyDate","1900-01-01")))
            # 审核人
            # model["F_UNW_APPROVEUserId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_APPROVEUserId",""))}
            # 审核日期
            # model["F_UNW_APPROVEDate"] = self.replace_none_date(str(row.get("F_UNW_APPROVEDate","1900-01-01")))
            # 创建组织 (必填项)
            model["F_UNW_OrgId"] = {"FNumber": self.replace_none_str(row.get("F_UNW_OrgId", ""))}
            # 动账时间
            model["F_UNW_DATE"] = self.replace_none_date(str(row.get("F_UNW_DATE", "1900-01-01")))
            # 业务场景（必填项）
            model["F_UNW_YWCJ"] = self.replace_none_str(row.get("F_UNW_YWCJ", ""))
            # 【底】店铺名称
            model["F_UNW_DPMC"] = self.replace_none_str(row.get("F_UNW_DPMC", ""))
            # 店铺ID
            model["F_UNW_DPID1"] = self.replace_none_str(row.get("F_UNW_DPID1", ""))
            # 销售主体 (必填项)
            model["F_UNW_XSZT"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_XSZT", ""))}
            # 平台编码
            model["F_UNW_PT1"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT1", ""))}
            # 是否同步税务账
            # model["F_XYS_ISSWZ"] = row.get("F_XYS_ISSWZ","false")

        #退货供货价表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_thghjb':
            # 实体主键
            # model["FID"] = self.replace_none_numb(row.get("FID",0))
            # 单据编号，已统一映射
            # model["FBillNo"] = self.replace_none_str(row.get("",""))
            # 创建人
            #model["F_UNW_CreatorId"] = {"FUserID": self.replace_none_str(row.get("F_UNW_CreatorId", ""))}
            # 创建日期
            # model["F_UNW_CreateDate"] = self.replace_none_date(str(row.get("F_UNW_CreateDate","1900-01-01")))
            # 修改人
            # model["F_UNW_ModifierId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_ModifierId",""))}
            # 修改日期
            # model["F_UNW_ModifyDate"] = self.replace_none_date(str(row.get("F_UNW_ModifyDate","1900-01-01")))
            # 审核人
            # model["F_UNW_APPROVEUserId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_APPROVEUserId",""))}
            # 审核日期
            # model["F_UNW_APPROVEDate"] = self.replace_none_date(str(row.get("F_UNW_APPROVEDate","1900-01-01")))
            # 创建组织 (必填项)
            model["F_UNW_OrgId"] = {"FNumber": self.replace_none_str(row.get("F_UNW_OrgId", ""))}
            # 入账时间
            model["F_UNW_DATE"] = self.replace_none_date(str(row.get("F_UNW_DATE", "1900-01-01")))
            # 【底】店铺名称
            model["F_UNW_DPMC"] = self.replace_none_str(row.get("F_UNW_DPMC", ""))
            # 销售主体 (必填项)
            model["F_UNW_XSZT"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_XSZT", ""))}
            # 平台编码
            model["F_UNW_PT1"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT1", ""))}
            # 是否同步税务账
            # model["F_XYS_ISSWZ"] = row.get("F_XYS_ISSWZ","false")

        # 保费明细表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_insurance':
            # 实体主键
            # model["FID"] = self.replace_none_numb(row.get("FID",0))
            # 单据编号，已统一映射
            # model["FBillNo"] = self.replace_none_str(row.get("",""))
            # 创建人
            #model["F_UNW_CreatorId"] = {"FUserID": self.replace_none_str(row.get("F_UNW_CreatorId", ""))}
            # 创建日期
            # model["F_UNW_CreateDate"] = self.replace_none_date(str(row.get("F_UNW_CreateDate","1900-01-01")))
            # 修改人
            # model["F_UNW_ModifierId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_ModifierId",""))}
            # 修改日期
            # model["F_UNW_ModifyDate"] = self.replace_none_date(str(row.get("F_UNW_ModifyDate","1900-01-01")))
            # 审核人
            # model["F_UNW_APPROVEUserId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_APPROVEUserId",""))}
            # 审核日期
            # model["F_UNW_APPROVEDate"] = self.replace_none_date(str(row.get("F_UNW_APPROVEDate","1900-01-01")))
            # 创建组织 (必填项)
            model["F_UNW_OrgId"] = {"FNumber": self.replace_none_str(row.get("F_UNW_OrgId", ""))}
            # 动账时间
            model["F_UNW_DATE"] = self.replace_none_date(str(row.get("F_UNW_DATE", "1900-01-01")))
            # 店铺ID
            model["F_UNW_DPID2"] = self.replace_none_str(row.get("F_UNW_DPID2", ""))
            # 销售主体 (必填项)
            model["F_UNW_XSZT"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_XSZT", ""))}
            # 平台编码
            model["F_UNW_PT1"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT1", ""))}
            # 是否同步税务账
            # model["F_XYS_ISSWZ"] = row.get("F_XYS_ISSWZ","false")

        # 优惠券差额表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_coupon':
            # 实体主键
            # model["FID"] = self.replace_none_numb(row.get("FID",0))
            # 单据编号，已统一映射
            # model["FBillNo"] = self.replace_none_str(row.get("",""))
            # 创建人
            #model["F_UNW_CreatorId"] = {"FUserID": self.replace_none_str(row.get("F_UNW_CreatorId", ""))}
            # 创建日期
            # model["F_UNW_CreateDate"] = self.replace_none_date(str(row.get("F_UNW_CreateDate","1900-01-01")))
            # 修改人
            # model["F_UNW_ModifierId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_ModifierId",""))}
            # 修改日期
            # model["F_UNW_ModifyDate"] = self.replace_none_date(str(row.get("F_UNW_ModifyDate","1900-01-01")))
            # 审核人
            # model["F_UNW_APPROVEUserId"] = {"FUserID":self.replace_none_str(row.get("F_UNW_APPROVEUserId",""))}
            # 审核日期
            # model["F_UNW_APPROVEDate"] = self.replace_none_date(str(row.get("F_UNW_APPROVEDate","1900-01-01")))
            # 创建组织 (必填项)
            model["F_UNW_OrgId"] = {"FNumber": self.replace_none_str(row.get("F_UNW_OrgId", ""))}
            # 静态日期
            model["F_UNW_DATE"] = self.replace_none_date(str(row.get("F_UNW_DATE", "1900-01-01")))
            # 店铺ID
            model["F_UNW_DPID1"] = self.replace_none_str(row.get("F_UNW_DPID1", ""))
            # 【底】店铺名称
            model["F_UNW_DPMC"] = self.replace_none_str(row.get("F_UNW_DPMC1", ""))
            # 销售主体 (必填项)
            model["F_UNW_XSZT"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_XSZT", ""))}
            # 平台编码
            model["F_UNW_PT1"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT1", ""))}
            # 是否同步税务账
            # model["F_XYS_ISSWZ"] = row.get("F_XYS_ISSWZ","false")

        # 聚水潭销售出库中间表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_sale_out':
            # 实体主键
            model["FID"] = self.replace_none_numb(row.get("FBillHead",0))
            # 单据编号，已统一映射
            # model["FBillNo"] = self.replace_none_str(row.get("",""))
            # model["FIsSuccess"] = self.replace_none_str(row.get("","false"))
            # model["F_XYS_BillType"] = self.replace_none_str(row.get("F_XYS_BillType", ""))
            # model["F_XYS_IsExe"] = self.replace_none_str(row.get("F_XYS_IsExe","false"))
            # 店铺
            model["F_XYS_ShopID"] = {"FNUMBER": self.replace_none_str(row.get("F_XYS_ShopID", ""))}
            # 组织
            model["F_XYS_OrgId"] = {"FNumber": self.replace_none_str(row.get("F_XYS_OrgId", ""))}
            # 内部单号
            model["FSrcBillNo"] = self.replace_none_str(row.get("FSrcBillNo", ""))
            model["Flabels"] = self.replace_none_str(row.get("Flabels", ""))
            # 客户(必填项)
            model["FCUSTID"] = {"FNUMBER": self.replace_none_str(row.get("FCUSTID", ""))}
            model["F_XYS_Date"] = self.replace_none_date(str(row.get("F_XYS_Date", "1900-01-01")))
            model["FNOTE"] = self.replace_none_str(row.get("FNOTE", ""))
            # model["FAllDisCount"] = self.replace_none_numb(row.get("FAllDisCount", 0))
            model["FJSTStock"] = self.replace_none_str(row.get("FJSTStock", ""))
            # 金蝶仓库(必填项)
            model["F_XYS_StokID"] = {"FNUMBER": self.replace_none_str(row.get("F_XYS_StokID", ""))}
            model["F_XYS_OUTNO"] = self.replace_none_str(row.get("F_XYS_OUTNO", ""))
            model["F_XYS_ordertype"] = self.replace_none_str(row.get("F_XYS_ordertype", ""))
            model["F_XYS_so_id"] = self.replace_none_str(row.get("F_XYS_so_id", ""))
            model["F_XYS_type"] = self.replace_none_str(row.get("F_XYS_type", ""))
            # model["F_XYS_supplier"] = {"FNUMBER": self.replace_none_str(row.get("F_XYS_supplier", ""))}
            # model["F_XYS_poid"] = self.replace_none_str(row.get("F_XYS_poid", ""))
            # model["F_XYS_OutBillNO"] = self.replace_none_str(row.get("F_XYS_OutBillNO", ""))
            # model["F_XYS_UnitID"] = {"FNumber": self.replace_none_str(row.get("F_XYS_UnitID", ""))}

        # 聚水潭销售退货中间表（金蝶测试251010）
        elif getattr(self, 'table_name', '') == 'kd_sale_return':
            # 实体主键
            model["FID"] = self.replace_none_numb(row.get("FBillHead",0))
            # 单据编号，已统一映射
            # model["FBillNo"] = self.replace_none_str(row.get("FBillNo", ""))
            model["F_XYS_ShopID"] = {"FNUMBER": self.replace_none_str(row.get("F_XYS_ShopID", ""))}
            model["F_XYS_OrgId"] = {"FNumber": self.replace_none_str(row.get("F_XYS_OrgId", ""))}
            model["FSrcBillNo"] = self.replace_none_str(row.get("FSrcBillNo", ""))
            model["Flabels"] = self.replace_none_str(row.get("Flabels", ""))
            model["FCUSTID"] = {"FNUMBER": self.replace_none_str(row.get("FCUSTID", ""))}
            model["F_XYS_Date"] = self.replace_none_date(str(row.get("F_XYS_Date", "1900-01-01")))
            model["FNOTE"] = self.replace_none_str(row.get("FNOTE", ""))
            # model["FAllDisCount"] = self.replace_none_numb(row.get("FAllDisCount", 0))
            model["FJSTStock"] = self.replace_none_str(row.get("FJSTStock", ""))
            model["F_XYS_StokID"] = {"FNUMBER": self.replace_none_str(row.get("F_XYS_StokID", ""))}
            model["F_XYS_ordertype"] = self.replace_none_str(row.get("F_XYS_ordertype", ""))
            model["F_XYS_so_id"] = self.replace_none_str(row.get("F_XYS_so_id", ""))
            # model["F_XYS_IsExe"] = self.replace_none_str(row.get("F_XYS_IsExe","false"))
            model["F_XYS_OutBillNO"] = self.replace_none_str(row.get("F_XYS_OutBillNO", ""))

        # 应付单（运费）
        elif getattr(self, 'table_name', '') == 'kd_ship_fee':
            # *单据头(序号)
            model["FBillHead"] = self.replace_none_str(row.get("FBillHead", ""))
            # *(单据头)单据类型#编码
            model["FBillTypeID"] = {"FNumber": self.replace_none_str(row.get("FBillTypeID", ""))}
            # (单据头)单据类型#名称
            model["FBillTypeIDName"] = self.replace_none_str(row.get("FBillTypeIDName", ""))
            # (单据头)单据编号
            model["FBillNo"] = self.replace_none_str(row.get("FBillNo", ""))
            # *(单据头)业务日期
            model["FDATE"] = self.replace_none_date(str(row.get("FDATE", "1900-01-01")))
            # *(单据头)供应商#编码
            model["FSUPPLIERID"] = {"FNumber": self.replace_none_str(row.get("FSUPPLIERID", ""))}
            # (单据头)供应商#名称
            model["FSUPPLIERIDName"] = self.replace_none_str(row.get("FSUPPLIERIDName", ""))
            # *(单据头)币别#编码
            model["FCURRENCYID"] = {"FNumber": self.replace_none_str(row.get("FCURRENCYID", ""))}
            # (单据头)币别#名称
            model["FCURRENCYIDName"] = self.replace_none_str(row.get("FCURRENCYIDName", ""))
            # *(单据头)业务类型
            model["FBUSINESSTYPE"] = self.replace_none_str(row.get("FBUSINESSTYPE", ""))
            # *(单据头)付款组织#编码
            model["FPAYORGID"] = {"FNumber": self.replace_none_str(row.get("FPAYORGID", ""))}
            # (单据头)付款组织#名称
            model["FPAYORGIDName"] = self.replace_none_str(row.get("FPAYORGIDName", ""))
            # *(单据头)结算组织#编码
            model["FSETTLEORGID"] = {"FNumber": self.replace_none_str(row.get("FSETTLEORGID", ""))}
            # (单据头)结算组织#名称
            model["FSETTLEORGIDName"] = self.replace_none_str(row.get("FSETTLEORGIDName", ""))
            # *(单据头)支付主体#编码
            model["F_UNW_CGZT"] = {"FNumber": self.replace_none_str(row.get("F_UNW_CGZT", ""))}
            # *(单据头)支付主体#名称
            model["F_UNW_CGZTName"] = self.replace_none_str(row.get("F_UNW_CGZTName", ""))
            # (单据头)税务账供应商#编码
            model["F_UNW_SWZGYS"] = {"FNumber": self.replace_none_str(row.get("F_UNW_SWZGYS", ""))}
            # (单据头)税务账供应商#名称
            model["F_UNW_SWZGYSName"] = self.replace_none_str(row.get("F_UNW_SWZGYSName", ""))

    # 映射单据分录字段 FEntity 【新】
    def _new_map_entity_fields_api_style(self, entity_item, row):
        """
                entity_item[""] = row.get("", "")
                entity_item[""] = {"FUserID": row.get("", "")}
                entity_item[""] = {"FNumber": row.get("", "")}
                entity_item[""] = {"FNUMBER": row.get("", "")}
                """
        #订单明细表（金蝶测试251002）
        if getattr(self, 'table_name', '') == 'kd_order_detail':
            # 实体主键
            #entity_item["FEntryID"] = self.replace_none_numb(row.get("", 0))
            # 平台编码
            entity_item["F_UNW_PT"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT", ""))}
            # 店铺ID-禁用
            entity_item["F_UNW_DPID"] = self.replace_none_str(row.get("F_UNW_DPID", ""))
            # 【底】店铺名称-禁用
            entity_item["F_UNW_DDPMC"] = self.replace_none_str(row.get("F_UNW_DDPMC", ""))
            # 主订单编号
            entity_item["F_UNW_ZDDBH"] = self.replace_none_str(row.get("F_UNW_ZDDBH", ""))
            # 子订单编号
            entity_item["F_UNW_ZIDDBH"] = self.replace_none_str(row.get("F_UNW_ZIDDBH", ""))
            # 选购商品
            entity_item["F_UNW_XGSP"] = self.replace_none_str(row.get("F_UNW_XGSP", ""))
            # 商品规格
            entity_item["F_UNW_SPGG"] = self.replace_none_str(row.get("F_UNW_SPGG", ""))
            #售卖方式
            entity_item["F_UNW_SMFS"] = self.replace_none_str(row.get("F_UNW_SMFS", ""))
            # 商家编码（商家后台商品编码）
            entity_item["F_UNW_SJBM"] = self.replace_none_str(row.get("F_UNW_SJBM", ""))
            # 商品数量
            entity_item["F_UNW_SPSL"] = self.replace_none_numb(row.get("F_UNW_SPSL", 0))
            # 商品单价
            entity_item["F_UNW_SPDJ"] = self.replace_none_numb(row.get("F_UNW_SPDJ", 0))
            # 订单应付金额
            entity_item["F_UNW_DDYFJE"] = self.replace_none_numb(row.get("F_UNW_DDYFJE", 0))
            # 实际运费
            entity_item["F_UNW_SJYF"] = self.replace_none_numb(row.get("F_UNW_SJYF", 0))
            # 优惠总金额
            entity_item["F_UNW_YHZJE"] = self.replace_none_numb(row.get("F_UNW_YHZJE", 0))
            # 平台优惠
            entity_item["F_UNW_PTYH"] = self.replace_none_numb(row.get("F_UNW_PTYH", 0))
            # 商家优惠
            entity_item["F_UNW_SJYH"] = self.replace_none_numb(row.get("F_UNW_SJYH", 0))
            # 达人优惠
            entity_item["F_UNW_DRYH"] = self.replace_none_numb(row.get("F_UNW_DRYH", 0))
            # 商家改价
            entity_item["F_UNW_SJGJ"] = self.replace_none_numb(row.get("F_UNW_SJGJ", 0))
            # 支付优惠
            entity_item["F_UNW_ZFYH"] = self.replace_none_numb(row.get("F_UNW_ZFYH", 0))
            # 红包抵扣
            entity_item["F_UNW_HBDK"] = self.replace_none_numb(row.get("F_UNW_HBDK", 0))
            # 支付方式
            entity_item["F_UNW_ZFFS"] = self.replace_none_str(row.get("F_UNW_ZFFS", ""))
            # 下单日期
            entity_item["F_UNW_XDRQ"] = self.replace_none_date(str(row.get("F_UNW_XDRQ", "1900-01-01")))
            # 最新商家备注
            entity_item["F_UNW_ZXSJBZ"] = self.replace_none_str(row.get("F_UNW_ZXSJBZ", ""))
            # 订单完成时间
            entity_item["F_UNW_DDWCSJ"] = self.replace_none_date(str(row.get("F_UNW_DDWCSJ", "1900-01-01")))
            # 支付完成日期
            entity_item["F_UNW_ZFWCRQ"] = self.replace_none_date(str(row.get("F_UNW_ZFWCRQ", "1900-01-01")))
            # APP渠道
            entity_item["F_UNW_APPQD"] = self.replace_none_str(row.get("F_UNW_APPQD", ""))
            # 流量来源
            entity_item["F_UNW_LLLY"] = self.replace_none_str(row.get("F_UNW_LLLY", ""))
            # 流量类型
            entity_item["F_UNW_LLLX"] = self.replace_none_str(row.get("F_UNW_LLLX", ""))
            # 订单状态描述
            entity_item["F_UNW_DDZTMS"] = self.replace_none_str(row.get("F_UNW_DDZTMS", ""))
            # 达人ID
            entity_item["F_UNW_DRID"] = self.replace_none_str(row.get("F_UNW_DRID", ""))
            # 【底】达人代码
            entity_item["F_UNW_DDRDM"] = self.replace_none_str(row.get("F_UNW_DDRDM", ""))
            # 达人昵称
            entity_item["F_UNW_DRNC"] = self.replace_none_str(row.get("F_UNW_DRNC", ""))
            # 售后状态
            entity_item["F_UNW_SHZT"] = self.replace_none_str(row.get("F_UNW_SHZT", ""))
            # 发货时间
            entity_item["F_UNW_FHSJ"] = self.replace_none_date(str(row.get("F_UNW_FHSJ", "1900-01-01")))
            # 降价类优惠
            entity_item["F_UNW_JJLYH"] = self.replace_none_numb(row.get("F_UNW_JJLYH", 0))
            # 平台实际承担优惠
            entity_item["F_UNW_PTSJCDYH"] = self.replace_none_numb(row.get("F_UNW_PTSJCDYH", 0))
            # 商家实际承担优惠
            entity_item["F_UNW_SJSJCDYH"] = self.replace_none_numb(row.get("F_UNW_SJSJCDYH", 0))
            # 达人实际承担优惠
            entity_item["F_UNW_DRSJCDYH"] = self.replace_none_numb(row.get("F_UNW_DRSJCDYH", 0))
            entity_item["F_UNW_KDGSMC"] = self.replace_none_str(row.get("F_UNW_KDGSMC", ""))
            entity_item["F_UNW_WLDH"] = self.replace_none_str(row.get("F_UNW_WLDH", ""))
            entity_item["F_UNW_KDXX"] = self.replace_none_str(row.get("F_UNW_KDXX", ""))
            entity_item["F_UNW_SJYH1"] = self.replace_none_numb(row.get("F_UNW_SJYH", 0))
            entity_item["F_UNW_YSJE"] = self.replace_none_numb(row.get("F_UNW_YSJE", 0))
            entity_item["F_UNW_YSZJE"] = self.replace_none_numb(row.get("F_UNW_YSZJE", 0))
            entity_item["F_UNW_CPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_CPNBGHJ", 0))
            entity_item["F_UNW_ZPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_ZPNBGHJ", 0))
            entity_item["F_UNW_HZCPCB"] = self.replace_none_numb(row.get("F_UNW_HZCPCB", 0))
            entity_item["F_UNW_HZJSJE"] = self.replace_none_numb(row.get("F_UNW_HZJSJE", 0))
            entity_item["F_UNW_SSYHQWRZ"] = self.replace_none_numb(row.get("F_UNW_SSYHQWRZ", 0))
            entity_item["F_UNW_DXMDM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DXMDM", ""))}
            entity_item["F_UNW_DSPBB"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DSPBB", ""))}
            entity_item["F_UNW_DCPLX"] = self.replace_none_str(row.get("F_UNW_DCPLX", ""))
            entity_item["F_UNW_DCPPL"] = self.replace_none_str(row.get("F_UNW_DCPPL", ""))
            entity_item["F_UNW_DDPZT"] = self.replace_none_str(row.get("F_UNW_DDPZT", ""))
            entity_item["F_UNW_DJXSL"] = self.replace_none_numb(row.get("F_UNW_DJXSL", 0))
            entity_item["F_UNW_DXXSL"] = self.replace_none_numb(row.get("F_UNW_DXXSL", 0))
            entity_item["F_UNW_BMDM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BMDM", ""))}
            entity_item["F_UNW_XJSJH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_XJSJH", ""))}
            entity_item["F_UNW_CPDL"] = {"FNumber": self.replace_none_str(row.get("F_UNW_CPDL", ""))}
            entity_item["F_UNW_PTZH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PTZH", ""))}
            entity_item["F_UNW_GSY"] = {"FNumber": self.replace_none_str(row.get("F_UNW_GSY", ""))}
            entity_item["F_UNW_BB"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BB", ""))}
            entity_item["F_UNW_HZCPGYS"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_HZCPGYS", ""))}
            entity_item["F_UNW_HZCPZPGYS"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_HZCPZPGYS", ""))}
            entity_item["F_UNW_KCWL"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_KCWL", ""))}
            entity_item["F_UNW_ZHCFMSSJ"] = self.replace_none_numb(row.get("F_UNW_ZHCFMSSJ", 0))
            entity_item["F_UNW_ZHCFYSSJ"] = self.replace_none_numb(row.get("F_UNW_ZHCFYSSJ", 0))
            entity_item["F_UNW_ZHCFFW"] = self.replace_none_numb(row.get("F_UNW_ZHCFFW", 0))
            entity_item["F_UNW_XXSLCFMSSJ"] = self.replace_none_numb(row.get("F_UNW_XXSLCFMSSJ", 0))
            entity_item["F_UNW_XSSLCFYSSJ"] = self.replace_none_numb(row.get("F_UNW_XSSLCFYSSJ", 0))
            entity_item["F_UNW_XXSLCFFW"] = self.replace_none_numb(row.get("F_UNW_XXSLCFFW", 0))
            #entity_item["F_XYS_OtherZTID"] = self.replace_none_numb(row.get("", 0))
            #entity_item["F_XYS_OtherZTEntryID"] = self.replace_none_numb(row.get("", 0))
            #entity_item["F_XYS_OtherZTBillNO"] = self.replace_none_str(row.get("", ""))
            entity_item["F_UNW_ZGGYXM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_ZGGYXM", ""))}
            entity_item["F_UNW_ZGGYBL"] = self.replace_none_numb(row.get("F_UNW_ZGGYBL", 0))

        #退货明细表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_order_return':
            # 实体主键
            #entity_item["FEntryID"] = self.replace_none_numb(row.get("FEntryID",0))
            # 平台编码
            entity_item["F_UNW_PT"] = {"FNumber":self.replace_none_str(row.get("F_UNW_PT",""))}
            # 店铺ID-禁用
            entity_item["F_UNW_DPID"] = self.replace_none_str(row.get("F_UNW_DPID1", ""))
            # 【底】店铺名称-禁用
            entity_item["F_UNW_DDPMC"] = self.replace_none_str(row.get("F_UNW_DDPMC", ""))
            # 主订单编号
            entity_item["F_UNW_ZDDBH"] = self.replace_none_str(row.get("F_UNW_ZDDBH", ""))
            # 子订单编号
            entity_item["F_UNW_ZIDDBH"] = self.replace_none_str(row.get("F_UNW_ZIDDBH", ""))
            # 选购商品
            entity_item["F_UNW_XGSP"] = self.replace_none_str(row.get("F_UNW_XGSP", ""))
            # 商品规格
            entity_item["F_UNW_SPGG"] = self.replace_none_str(row.get("F_UNW_SPGG", ""))
            # 商家编码（商家后台商品编码）
            entity_item["F_UNW_SJBM"] = self.replace_none_str(row.get("F_UNW_SJBM", ""))
            # 【底】达人代码
            entity_item["F_UNW_DDRDM"] = self.replace_none_str(row.get("F_UNW_DDRDM", ""))
            # 达人ID
            entity_item["F_UNW_DRID"] = self.replace_none_str(row.get("F_UNW_DRID", ""))
            # 达人昵称
            entity_item["F_UNW_DRNC"] = self.replace_none_str(row.get("F_UNW_DRNC", ""))
            # 支付日期
            entity_item["F_UNW_ZFRQ"] = self.replace_none_date(str(row.get("F_UNW_ZFRQ", "1900-01-01")))
            # 下单日期
            entity_item["F_UNW_XDRQ"] = self.replace_none_date(str(row.get("F_UNW_XDRQ", "1900-01-01")))
            # 商品数量
            entity_item["F_UNW_SPSL"] = self.replace_none_numb(row.get("F_UNW_SPSL",0))
            # 商品单价
            entity_item["F_UNW_SPDJ"] = self.replace_none_numb(row.get("F_UNW_SPDJ",0))
            # 订单应付金额
            entity_item["F_UNW_DDYFJE"] = self.replace_none_numb(row.get("F_UNW_DDYFJE",0))
            # 实际运费
            entity_item["F_UNW_SJYF"] = self.replace_none_numb(row.get("F_UNW_SJYF",0))
            # 优惠总金额
            entity_item["F_UNW_YHZJE"] = self.replace_none_numb(row.get("F_UNW_YHZJE",0))
            # 平台优惠
            entity_item["F_UNW_PTYH"] = self.replace_none_numb(row.get("F_UNW_PTYH",0))
            # 商家优惠
            entity_item["F_UNW_SJYH"] = self.replace_none_numb(row.get("F_UNW_SJYH",0))
            # 达人优惠
            entity_item["F_UNW_DRYH"] = self.replace_none_numb(row.get("F_UNW_DRYH",0))
            # 商家改价
            entity_item["F_UNW_SJGJ"] = self.replace_none_numb(row.get("F_UNW_SJGJ",0))
            # 支付优惠
            entity_item["F_UNW_ZFYH"] = self.replace_none_numb(row.get("F_UNW_ZFYH",0))
            # 红包抵扣
            entity_item["F_UNW_HBDK"] = self.replace_none_numb(row.get("F_UNW_HBDK",0))
            # 支付方式
            entity_item["F_UNW_ZFFS"] = self.replace_none_str(row.get("F_UNW_ZFFS", ""))
            # 最新商家备注
            entity_item["F_UNW_ZXSJBZ"] = self.replace_none_str(row.get("F_UNW_ZXSJBZ", ""))
            # 订单完成时间
            entity_item["F_UNW_DDWCSJ"] = self.replace_none_date(str(row.get("F_UNW_DDWCSJ", "1900-01-01")))
            # 支付完成日期
            #entity_item["F_UNW_ZFWCRQ"] = self.replace_none_date(str(row.get("F_UNW_ZFWCRQ", "1900-01-01")))
            # APP渠道
            entity_item["F_UNW_APPQD"] = self.replace_none_str(row.get("F_UNW_APPQD", ""))
            # 流量来源
            entity_item["F_UNW_LLLY"] = self.replace_none_str(row.get("F_UNW_LLLY", ""))
            # 订单状态描述
            entity_item["F_UNW_DDZTMS"] = self.replace_none_str(row.get("F_UNW_DDZTMS", ""))
            # 售后状态
            entity_item["F_UNW_SHZT"] = self.replace_none_str(row.get("F_UNW_SHZT", ""))
            # 发货时间
            entity_item["F_UNW_FHSJ"] = self.replace_none_date(str(row.get("F_UNW_FHSJ", "1900-01-01")))
            # 降价类优惠
            entity_item["F_UNW_JJLYH"] = self.replace_none_numb(row.get("F_UNW_JJLYH",0))
            # 平台实际承担优惠
            entity_item["F_UNW_PTSJCDYH"] = self.replace_none_numb(row.get("F_UNW_PTSJCDYH",0))
            # 商家实际承担优惠
            entity_item["F_UNW_SJSJCDYH"] = self.replace_none_numb(row.get("F_UNW_SJSJCDYH",0))
            # 达人实际承担优惠
            entity_item["F_UNW_DRSJCDYH"] = self.replace_none_numb(row.get("F_UNW_DRSJCDYH",0))
            # 快递公司名称
            entity_item["F_UNW_KDGSMC"] = self.replace_none_str(row.get("F_UNW_KDGSMC", ""))
            # 物流单号
            entity_item["F_UNW_WLDH"] = self.replace_none_str(row.get("F_UNW_WLDH", ""))
            # 快递信息
            entity_item["F_UNW_KDXX"] = self.replace_none_str(row.get("F_UNW_KDXX", ""))
            # 商家优惠
            entity_item["F_UNW_SJYH1"] = self.replace_none_numb(row.get("F_UNW_SJYH1",0))
            # 应收金额
            entity_item["F_UNW_YSJE"] = self.replace_none_numb(row.get("F_UNW_YSJE",0))
            # 应收总金额
            entity_item["F_UNW_YSZJE"] = self.replace_none_numb(row.get("F_UNW_YSZJE",0))
            # 结算前可退金额
            entity_item["F_UNW_JSQKTJE"] = self.replace_none_numb(row.get("F_UNW_JSQKTJE",0))
            # 结算金额
            entity_item["F_UNW_JSJE"] = self.replace_none_numb(row.get("F_UNW_JSJE",0))
            # 合计退款金额
            entity_item["F_UNW_HETKJE"] = self.replace_none_numb(row.get("F_UNW_HETKJE",0))
            # 退款金额
            entity_item["F_UNW_TKJE"] = self.replace_none_numb(row.get("F_UNW_TKJE",0))
            # 售后退款金额
            entity_item["F_UNW_SHTKJE"] = self.replace_none_numb(row.get("F_UNW_SHTKJE",0))
            # 优惠券未入账金额
            entity_item["F_UNW_YHQFRZJE"] = self.replace_none_numb(row.get("F_UNW_YHQFRZJE",0))
            # 退货场景
            entity_item["F_UNW_THCJ"] = self.replace_none_str(row.get("F_UNW_THCJ", ""))
            # 售后类型
            entity_item["F_UNW_SHLX"] = self.replace_none_str(row.get("F_UNW_SHLX", ""))
            # 红冲方式
            entity_item["F_UNW_HCFS"] = self.replace_none_str(row.get("F_UNW_HCFS", ""))
            # 期初 特殊红冲状
            #entity_item["F_UNW_QCTSHZTT"] = self.replace_none_str(row.get("", ""))
            # 【底】项目代码
            entity_item["F_UNW_DXMDM"] = {"FNumber":self.replace_none_str(row.get("F_UNW_DXMDM",""))}
            # 【底】商品版本
            entity_item["F_UNW_DSPBB"] = {"FNumber":self.replace_none_str(row.get("F_UNW_DSPBB",""))}
            # 【底】税号
            entity_item["F_UNW_DSH"] = self.replace_none_str(row.get("F_UNW_DSH", ""))
            # 【底】销项税率
            entity_item["F_UNW_DXXSL"] = self.replace_none_numb(row.get("F_UNW_DXXSL",0))
            # 【底】进项税率
            entity_item["F_UNW_DJXSL"] = self.replace_none_numb(row.get("F_UNW_DJXSL",0))
            # 部门代码
            entity_item["F_UNW_BMDM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BMDM", ""))}
            # 发起部门
            entity_item["F_UNW_FQBM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_FQBM", ""))}
            # 费用管理部门
            entity_item["F_UNW_FYGLBM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_FYGLBM", ""))}
            # 小店手机号
            entity_item["F_UNW_XJSJH"] = {"FNumber":self.replace_none_str(row.get("F_UNW_XJSJH",""))}
            # 行业
            entity_item["F_UNW_CPDL"] = {"FNumber":self.replace_none_str(row.get("F_UNW_CPDL",""))}
            # 平台账号（抖音-视频号-小红书号）
            entity_item["F_UNW_PTZH"] = {"FNumber":self.replace_none_str(row.get("F_UNW_PTZH",""))}
            # 公私域
            entity_item["F_UNW_GSY"] = {"FNumber":self.replace_none_str(row.get("F_UNW_GSY",""))}
            # 币别（必填项）
            entity_item["F_UNW_BB"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BB", ""))}
            # 合作产品供应商
            entity_item["F_UNW_HZCPGYS"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_HZCPGYSNAME", ""))}
            # 合作产品赠品供应商
            entity_item["F_UNW_HZCPZPGYS"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_HZCPZPGYSNAME", ""))}
            # 合作产品/赠品库存物料编码
            entity_item["F_UNW_KCWL"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_KCWLNAME", ""))}
            # 组合拆分-免税书籍
            entity_item["F_UNW_ZHCFMSSJ"] = self.replace_none_numb(row.get("F_UNW_ZHCFMSSJ",0))
            # 组合拆分-应税货物
            entity_item["F_UNW_ZHCFYSSJ"] = self.replace_none_numb(row.get("F_UNW_ZHCFYSSJ",0))
            # 组合拆分-服务
            entity_item["F_UNW_ZHCFFW"] = self.replace_none_numb(row.get("F_UNW_ZHCFFW",0))
            # 销项税率拆分-免税书籍
            entity_item["F_UNW_XXSLCFMSSJ"] = self.replace_none_numb(row.get("F_UNW_XXSLCFMSSJ",0))
            # 销项税率拆分-应税货物
            entity_item["F_UNW_XXSLCFYSSJ"] = self.replace_none_numb(row.get("F_UNW_XXSLCFYSSJ",0))
            # 销项税率拆分-服务
            entity_item["F_UNW_XXSLCFFW"] = self.replace_none_numb(row.get("F_UNW_XXSLCFFW",0))
            # 税务账单据ID
            #entity_item["F_XYS_OtherZTID"] = self.replace_none_numb(row.get("",0))
            # 税务账分录ID
            #entity_item["F_XYS_OtherZTEntryID"] = self.replace_none_numb(row.get("",0))
            # 税务账单据编号
            #entity_item["F_XYS_OtherZTBillNO"] =self.replace_none_str(row.get("", ""))
            # 转高公域项目编码
            entity_item["F_UNW_ZGGYXM"] = {"FNumber":self.replace_none_str(row.get("F_UNW_ZGGYXM",""))}
            # 转高公域比例
            entity_item["F_UNW_ZGGYBL"] = self.replace_none_numb(row.get("F_UNW_ZGGYBL",0))

        # 动账明细表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_dzmx':
            #entity_item["FEntryID"] = self.replace_none_numb(row.get("FEntryID", 0))
            entity_item["F_UNW_PT"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT", ""))}
            entity_item["F_UNW_DPID"] = self.replace_none_str(row.get("F_UNW_DPID", ""))
            entity_item["F_UNW_DDPMC"] = self.replace_none_str(row.get("F_UNW_DDPMC", ""))
            entity_item["F_UNW_DZLSH"] = self.replace_none_str(row.get("F_UNW_DZLSH", ""))
            entity_item["F_UNW_DZFX"] = self.replace_none_str(row.get("F_UNW_DZFX", ""))
            entity_item["F_UNW_DZJE"] = self.replace_none_numb(row.get("F_UNW_DZJE", 0))
            entity_item["F_UNW_DZZH"] = self.replace_none_str(row.get("F_UNW_DZZH", ""))
            entity_item["F_UNW_DZZY"] = self.replace_none_str(row.get("F_UNW_DZZY", ""))
            entity_item["F_UNW_JFLXMS"] = self.replace_none_str(row.get("F_UNW_JFLXMS", ""))
            entity_item["F_UNW_ZIDDBH"] = self.replace_none_str(row.get("F_UNW_ZIDDBH", ""))
            entity_item["F_UNW_ZDDBH"] = self.replace_none_str(row.get("F_UNW_ZDDBH", ""))
            entity_item["F_UNW_XSSL"] = self.replace_none_numb(row.get("F_UNW_XSSL", 0))
            entity_item["F_UNW_DPZT"] = self.replace_none_str(row.get("F_UNW_XSZT", ""))
            entity_item["F_UNW_ZTNSSX"] = self.replace_none_str(row.get("F_UNW_ZTNSSX", ""))
            entity_item["F_UNW_SHBH"] = self.replace_none_str(row.get("F_UNW_SHBH", ""))
            entity_item["F_UNW_XDSJ"] = self.replace_none_date(str(row.get("F_UNW_XDSJ", "1900-01-01")))
            entity_item["F_UNW_SPMC"] = self.replace_none_str(row.get("F_UNW_SPMC", ""))
            entity_item["F_UNW_DRID"] = self.replace_none_str(row.get("F_UNW_DRID", ""))
            entity_item["F_UNW_DRMC"] = self.replace_none_str(row.get("F_UNW_DRMC", ""))
            entity_item["F_UNW_DDLXMS"] = self.replace_none_str(row.get("F_UNW_DDLXMS", ""))
            entity_item["F_UNW_DDSFYJ"] = self.replace_none_numb(row.get("F_UNW_DDSFYJ", 0))
            entity_item["F_UNW_YFSF"] = self.replace_none_numb(row.get("F_UNW_YFSF", 0))
            entity_item["F_UNW_SJPTBTYF"] = self.replace_none_numb(row.get("F_UNW_SJPTBTYF", 0))
            entity_item["F_UNW_SJPTBT"] = self.replace_none_numb(row.get("F_UNW_SJPTBT", 0))
            entity_item["F_UNW_YJHXDK"] = self.replace_none_numb(row.get("F_UNW_YJHXDK", 0))
            entity_item["F_UNW_ZFBT"] = self.replace_none_numb(row.get("F_UNW_ZFBT", 0))
            entity_item["F_UNW_SJDRBT"] = self.replace_none_numb(row.get("F_UNW_SJDRBT", 0))
            entity_item["F_UNW_SJDYZFBT"] = self.replace_none_numb(row.get("F_UNW_SJDYZFBT", 0))
            entity_item["F_UNW_SJDYYFYXBT"] = self.replace_none_numb(row.get("F_UNW_SJDYYFYXBT", 0))
            entity_item["F_UNW_YHBT"] = self.replace_none_numb(row.get("F_UNW_YHBT", 0))
            entity_item["F_UNW_DDTK"] = self.replace_none_numb(row.get("F_UNW_DDTK", 0))
            entity_item["F_UNW_PTFWF"] = self.replace_none_numb(row.get("F_UNW_PTFWF", 0))
            entity_item["F_UNW_YJ"] = self.replace_none_numb(row.get("F_UNW_YJ", 0))
            entity_item["F_UNW_FWSYJ"] = self.replace_none_numb(row.get("F_UNW_FWSYJ", 0))
            entity_item["F_UNW_QDFC"] = self.replace_none_numb(row.get("F_UNW_QDFC", 0))
            entity_item["F_UNW_ZSFWF"] = self.replace_none_numb(row.get("F_UNW_ZSFWF", 0))
            entity_item["F_UNW_ZWTGF"] = self.replace_none_numb(row.get("F_UNW_ZWTGF", 0))
            entity_item["F_UNW_QTFC"] = self.replace_none_numb(row.get("F_UNW_QTFC", 0))
            entity_item["F_UNW_SFMY"] = self.replace_none_str(row.get("F_UNW_SFMY", ""))
            entity_item["F_UNW_MYJE"] = self.replace_none_numb(row.get("F_UNW_MYJE", 0))
            entity_item["F_UNW_KDGSMC"] = self.replace_none_str(row.get("F_UNW_KDGSMC", ""))
            entity_item["F_UNW_SRHJ"] = self.replace_none_numb(row.get("F_UNW_SRHJ", 0))
            #entity_item["F_UNW_UID"] = self.replace_none_str(row.get("F_UNW_UID", ""))
            entity_item["F_UNW_XMDM"] = self.replace_none_str(row.get("F_UNW_XMDM", ""))
            entity_item["F_UNW_CWBZ"] = self.replace_none_str(row.get("F_UNW_CWBZ", ""))
            entity_item["F_UNW_DSPBB"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DSPBB", ""))}
            entity_item["F_UNW_DDRDM"] = self.replace_none_str(row.get("F_UNW_DDRDM", ""))
            entity_item["F_UNW_DXMDM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DXMDM", ""))}
            entity_item["F_UNW_BMDM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BMDM", ""))}
            entity_item["F_UNW_BB"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BB", ""))}
            entity_item["F_UNW_CPDL"] = {"FNumber": self.replace_none_str(row.get("F_UNW_CPDL", ""))}
            entity_item["F_UNW_XDSJH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_XDSJH", ""))}
            entity_item["F_UNW_FQBM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_FQBM", ""))}
            entity_item["F_UNW_PTZH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PTZH", ""))}
            entity_item["F_UNW_FYGLBM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_FYGLBM", ""))}
            entity_item["F_UNW_GSY"] = {"FNumber": self.replace_none_str(row.get("F_UNW_GSY", ""))}
            entity_item["F_UNW_TZYJ"] = self.replace_none_numb(row.get("F_UNW_TZYJ", 0))
            entity_item["F_UNW_KZKYJ"] = self.replace_none_numb(row.get("F_UNW_KZKYJ", 0))
            entity_item["F_UNW_QTSF"] = self.replace_none_numb(row.get("F_UNW_QTSF", 0))
            entity_item["F_UNW_YSZSJSFWF"] = self.replace_none_numb(row.get("F_UNW_YSZSJSFWF", 0))
            entity_item["F_UNW_TZFWF"] = self.replace_none_numb(row.get("F_UNW_TZFWF", 0))
            entity_item["F_UNW_EBFWF"] = self.replace_none_numb(row.get("F_UNW_EBFWF", 0))
            entity_item["F_UNW_ZFSXF"] = self.replace_none_numb(row.get("F_UNW_ZFSXF", 0))
            entity_item["F_UNW_DDTKSJ"] = self.replace_none_date(str(row.get("F_UNW_DDTKSJ", "1900-01-01")))
            entity_item["F_UNW_TZID"] = self.replace_none_str(row.get("F_UNW_TZID", ""))
            entity_item["F_UNW_KZKID"] = self.replace_none_str(row.get("F_UNW_KZKID", ""))
            entity_item["F_UNW_QTSFMX"] = self.replace_none_str(row.get("F_UNW_QTSFMX", ""))
            entity_item["F_UNW_ZDLX"] = self.replace_none_str(row.get("F_UNW_ZDLX", ""))
            entity_item["F_UNW_ZGGYXM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_ZGGYXM", ""))}
            entity_item["F_UNW_ZGGYBL"] = self.replace_none_numb(row.get("F_UNW_ZGGYBL", 0))

        #退货供货价表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_thghjb':
            #entity_item["FEntryID"] = self.replace_none_numb(row.get("FEntryID", 0))
            entity_item["F_UNW_PT"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT", ""))}
            entity_item["F_UNW_ZIDDBH"] = self.replace_none_str(row.get("F_UNW_ZIDDBH", ""))
            entity_item["F_UNW_ZDDBH"] = self.replace_none_str(row.get("F_UNW_ZDDBH", ""))
            entity_item["F_UNW_SJBM"] = self.replace_none_str(row.get("F_UNW_SJBM", ""))
            entity_item["F_UNW_ZFRQ"] = self.replace_none_date(str(row.get("F_UNW_ZFRQ", "1900-01-01")))
            entity_item["F_UNW_BCFSRQ"] = self.replace_none_date(str(row.get("F_UNW_BCFSRQ", "1900-01-01")))
            entity_item["F_UNW_ZXSJBZ"] = self.replace_none_str(row.get("F_UNW_ZXSJBZ", ""))
            entity_item["F_UNW_DSPBB"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DSPBB", ""))}
            entity_item["F_UNW_DDRDM"] = self.replace_none_str(row.get("F_UNW_DDRDM", ""))
            entity_item["F_UNW_DXMDM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DXMDM", ""))}
            entity_item["F_UNW_JTRQ"] = self.replace_none_date(str(row.get("F_UNW_JTRQ", "1900-01-01")))
            entity_item["F_UNW_THCJ"] = self.replace_none_str(row.get("F_UNW_THCJ", ""))
            entity_item["F_UNW_BMDM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BMDM", ""))}
            entity_item["F_UNW_SPDJ"] = self.replace_none_numb(row.get("F_UNW_SPDJ", 0))
            entity_item["F_UNW_SPSL"] = self.replace_none_numb(row.get("F_UNW_SPSL", 0))
            entity_item["F_UNW_DDYFJE"] = self.replace_none_numb(row.get("F_UNW_DDYFJE", 0))
            entity_item["F_UNW_YSZJE"] = self.replace_none_numb(row.get("F_UNW_YSZJE", 0))
            entity_item["F_UNW_TKJE"] = self.replace_none_numb(row.get("F_UNW_TKJE", 0))
            entity_item["F_UNW_YHQFRZJE"] = self.replace_none_numb(row.get("F_UNW_YHQFRZJE", 0))
            entity_item["F_UNW_DCPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_DCPNBGHJ", 0))
            entity_item["F_UNW_ZDDCPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_ZDDCPNBGHJ", 0))
            entity_item["F_UNW_SJCPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_SJCPNBGHJ", 0))
            entity_item["F_UNW_JTCPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_JTCPNBGHJ", 0))
            entity_item["F_UNW_CECHNBGHJ"] = self.replace_none_numb(row.get("F_UNW_CECHNBGHJ", 0))
            entity_item["F_UNW_DZPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_DZPNBGHJ", 0))
            entity_item["F_UNW_ZDDZPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_ZDDZPNBGHJ", 0))
            entity_item["F_UNW_SSZPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_SSZPNBGHJ", 0))
            entity_item["F_UNW_JTZPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_JTZPNBGHJ", 0))
            entity_item["F_UNW_CEZPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_CEZPNBGHJ", 0))
            entity_item["F_UNW_ZDDHZCPCB"] = self.replace_none_numb(row.get("F_UNW_ZDDHZCPCB", 0))
            entity_item["F_UNW_SSHZCPCB"] = self.replace_none_numb(row.get("F_UNW_SSHZCPCB", 0))
            entity_item["F_UNW_JTHZCPCB"] = self.replace_none_numb(row.get("F_UNW_JTHZCPCB", 0))
            entity_item["F_UNW_CEHZCPCB"] = self.replace_none_numb(row.get("F_UNW_CEHZCPCB", 0))
            entity_item["F_UNW_SFSHDD"] = self.replace_none_str(row.get("F_UNW_SFSHDD", ""))
            entity_item["F_UNW_DXXSL"] = self.replace_none_numb(row.get("F_UNW_DXXSL", 0))
            entity_item["F_UNW_DJXSL"] = self.replace_none_numb(row.get("F_UNW_DJXSL", 0))
            entity_item["F_UNW_TKBL"] = self.replace_none_numb(row.get("F_UNW_TKBL", 0))
            entity_item["F_UNW_JLS"] = self.replace_none_numb(row.get("F_UNW_JLS", 0))
            entity_item["F_UNW_XJSJH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_XJSJH", ""))}
            entity_item["F_UNW_CPDL"] = {"FNumber": self.replace_none_str(row.get("F_UNW_CPDL", ""))}
            entity_item["F_UNW_PTZH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PTZH", ""))}
            entity_item["F_UNW_GSY"] = {"FNumber": self.replace_none_str(row.get("F_UNW_GSY", ""))}
            entity_item["F_UNW_BB"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BB", ""))}
            entity_item["F_UNW_HZCPGYS"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_HZCPGYS", ""))}
            entity_item["F_UNW_HZCPZPGYS"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_HZCPZPGYS", ""))}
            entity_item["F_UNW_KCWL"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_KCWL", ""))}

        #保费明细表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_insurance':
            #entity_item["FEntryID"] = self.replace_none_numb(row.get("FEntryID", 0))
            entity_item["F_UNW_PT"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT", ""))}
            entity_item["F_UNW_XDSJ"] = self.replace_none_date(str(row.get("F_UNW_XDSJ", "1900-01-01")))
            #entity_item["F_UNW_ZFSJ"] = self.replace_none_date(str(row.get("F_UNW_ZFSJ", "1900-01-01")))
            #entity_item["F_UNW_DCPLX"] = self.replace_none_str(row.get("F_UNW_DCPLX", ""))
            #entity_item["F_UNW_DCPPL"] = self.replace_none_str(row.get("F_UNW_DCPPL", ""))
            entity_item["F_UNW_DRDM"] = self.replace_none_str(row.get("F_UNW_DRDM", ""))
            entity_item["F_UNW_DDPZT"] = self.replace_none_str(row.get("F_UNW_DDPZT", ""))
            #entity_item["F_UNW_DFZHSLB"] = self.replace_none_str(row.get("F_UNW_DFZHSLB", ""))
            entity_item["F_UNW_DSPBB"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DSPBB", ""))}
            #entity_item["F_UNW_DSPXX"] = self.replace_none_str(row.get("F_UNW_DSPXX", ""))
            #entity_item["F_UNW_DSH"] = self.replace_none_str(row.get("F_UNW_DSH", ""))
            #entity_item["F_UNW_DSLBZ"] = self.replace_none_numb(row.get("F_UNW_DSLBZ", 0))
            entity_item["F_UNW_DXMDM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DXMDM", ""))}
            entity_item["F_UNW_BXMC"] = self.replace_none_str(row.get("F_UNW_BXMC", ""))
            entity_item["F_UNW_BZ"] = self.replace_none_str(row.get("F_UNW_BZ", ""))
            entity_item["F_UNW_DRID"] = self.replace_none_str(row.get("F_UNW_DRID", ""))
            entity_item["F_UNW_DPID1"] = self.replace_none_str(row.get("F_UNW_DPID2", ""))
            entity_item["F_UNW_DZLSH"] = self.replace_none_str(row.get("F_UNW_DZLSH", ""))
            entity_item["F_UNW_LLLY"] = self.replace_none_str(row.get("F_UNW_LLLY", ""))
            entity_item["F_UNW_SJBM"] = self.replace_none_str(row.get("F_UNW_SJBM", ""))
            entity_item["F_UNW_TBDH"] = self.replace_none_str(row.get("F_UNW_TBDH", ""))
            entity_item["F_UNW_ZDDH"] = self.replace_none_str(row.get("F_UNW_ZDDH", ""))
            # entity_item["F_UNW_DCPJCSL"] = self.replace_none_numb(row.get("F_UNW_DCPJCSL", 0))
            # entity_item["F_UNW_DCPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_DCPNBGHJ", 0))
            # entity_item["F_UNW_DJXSL"] = self.replace_none_numb(row.get("F_UNW_DJXSL", 0))
            # entity_item["F_UNW_DKPFWF"] = self.replace_none_numb(row.get("F_UNW_DKPFWF", 0))
            # entity_item["F_UNW_DXXSL1"] = self.replace_none_numb(row.get("F_UNW_DXXSL1", 0))
            # entity_item["F_UNW_DZPJCSL"] = self.replace_none_numb(row.get("F_UNW_DZPJCSL", 0))
            # entity_item["F_UNW_DZPNBGHJ"] = self.replace_none_numb(row.get("F_UNW_DZPNBGHJ", 0))
            # entity_item["F_UNW_CLBT"] = self.replace_none_numb(row.get("F_UNW_CLBT", 0))
            entity_item["F_UNW_ZFBF"] = self.replace_none_numb(row.get("F_UNW_ZFBF", 0))
            entity_item["F_UNW_PTZH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PTZH", ""))}
            entity_item["F_UNW_GSY"] = {"FNumber": self.replace_none_str(row.get("F_UNW_GSY", ""))}
            entity_item["F_UNW_BB"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BB", ""))}
            entity_item["F_UNW_CPDL"] = {"FNumber": self.replace_none_str(row.get("F_UNW_CPDL", ""))}
            entity_item["F_UNW_XDSJH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_XDSJH", ""))}
            entity_item["F_UNW_FQBM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_FQBM", ""))}
            # entity_item["F_XYS_OtherZTID"] = self.replace_none_numb(row.get("F_XYS_OtherZTID", 0))
            entity_item["F_UNW_FYGLBM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_FYGLBM", ""))}
            # entity_item["F_XYS_OtherZTEntryID"] = self.replace_none_numb(row.get("F_XYS_OtherZTEntryID", 0))
            # entity_item["F_XYS_OtherZTBillNO"] = self.replace_none_str(row.get("F_XYS_OtherZTBillNO", ""))

        #优惠券差额表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_coupon':
            #entity_item["FEntryID"] = self.replace_none_numb(row.get("FEntryID", 0))
            entity_item["F_UNW_PT"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT", ""))}
            entity_item["F_UNW_ZFRQ"] = self.replace_none_date(str(row.get("F_UNW_ZFRQ", "1900-01-01")))
            entity_item["F_UNW_ZDDBH"] = self.replace_none_str(row.get("F_UNW_ZDDBH", ""))
            entity_item["F_UNW_ZIDDBH"] = self.replace_none_str(row.get("F_UNW_ZIDDBH", ""))
            entity_item["F_UNW_DPID"] = self.replace_none_str(row.get("F_UNW_DPID", ""))
            entity_item["F_UNW_DDPMC"] = self.replace_none_str(row.get("F_UNW_DPMC1", ""))
            entity_item["F_UNW_DXMDM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DXMDM", ""))}
            entity_item["F_UNW_DDRDM"] = self.replace_none_str(row.get("F_UNW_DDRDM", ""))
            entity_item["F_UNW_DSPBB"] = {"FNumber": self.replace_none_str(row.get("F_UNW_DSPBB", ""))}
            entity_item["F_UNW_DXXSL"] = self.replace_none_numb(row.get("F_UNW_DXXSL", 0))
            entity_item["F_UNW_YHQWRZJE"] = self.replace_none_numb(row.get("F_UNW_YHQWRZJE", 0))
            entity_item["F_UNW_YHQWRZHJ"] = self.replace_none_numb(row.get("F_UNW_YHQWRZHJ", 0))
            entity_item["F_UNW_DDJTYHQWRZ"] = self.replace_none_numb(row.get("F_UNW_DDJTYHQWRZ", 0))
            entity_item["F_UNW_DYYHQCE"] = self.replace_none_numb(row.get("F_UNW_DYYHQCE", 0))
            entity_item["F_UNW_BMDM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BMDM", ""))}
            entity_item["F_UNW_XJSJH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_XJSJH", ""))}
            entity_item["F_UNW_CPDL"] = {"FNumber": self.replace_none_str(row.get("F_UNW_CPDL", ""))}
            entity_item["F_UNW_PTZH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PTZH", ""))}
            entity_item["F_UNW_GSY"] = {"FNumber": self.replace_none_str(row.get("F_UNW_GSY", ""))}
            entity_item["F_UNW_BB"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_BB", ""))}
            # entity_item["F_XYS_OtherZTID"] = self.replace_none_numb(row.get("F_XYS_OtherZTID", 0))
            # entity_item["F_XYS_OtherZTEntryID"] = self.replace_none_numb(row.get("F_XYS_OtherZTEntryID", 0))
            # entity_item["F_XYS_OtherZTBillNO"] = self.replace_none_str(row.get("F_XYS_OtherZTBillNO", ""))

        # 聚水潭销售出库中间表（金蝶测试251002）
        elif getattr(self, 'table_name', '') == 'kd_sale_out':
            entity_item["FEntryID"] = self.replace_none_numb(row.get("FEntity", 0))
            entity_item["FSkuID"] = self.replace_none_str(row.get("FSkuID", ""))
            entity_item["Fproperties_value"] = self.replace_none_str(row.get("Fproperties_value", ""))
            entity_item["FQTY"] = self.replace_none_numb(row.get("FQTY", 0))
            entity_item["FTAXPRICE"] = self.replace_none_numb(row.get("FTAXPRICE", 0))
            entity_item["Fsale_amount"] = self.replace_none_numb(row.get("Fsale_amount", 0))
            entity_item["FmaterialID"] = {"FNUMBER": self.replace_none_str(row.get("FmaterialID", ""))}
            entity_item["FzkAmount"] = self.replace_none_numb(row.get("FzkAmount", 0))
            entity_item["F_XYS_JSTEntryID"] = self.replace_none_str(row.get("F_XYS_JSTEntryID", ""))
            entity_item["F_XYS_skuname"] = self.replace_none_str(row.get("F_XYS_skuname", ""))
            entity_item["F_XYS_combine_sku_id"] = self.replace_none_str(row.get("F_XYS_combine_sku_id", ""))
            entity_item["F_XYS_is_gift"] = self.replace_none_str(row.get("F_XYS_is_gift", "false"))
            entity_item["F_XYS_PTZH"] = {"FNumber": self.replace_none_str(row.get("F_XYS_PTZH", ""))}
            entity_item["F_XYS_XM"] = {"FNumber": self.replace_none_str(row.get("F_XYS_XM", ""))}
            entity_item["F_XYS_GSY"] = {"FNumber": self.replace_none_str(row.get("F_XYS_GSY", ""))}
            entity_item["F_XYS_CPDL"] = {"FNumber": self.replace_none_str(row.get("F_XYS_CPDL", ""))}
            entity_item["F_XYS_PT"] = {"FNumber": self.replace_none_str(row.get("F_XYS_PT", ""))}
            entity_item["F_XYS_BM"] = {"FNUMBER": self.replace_none_str(row.get("F_XYS_BM", ""))}
            entity_item["F_XYS_SPMC"] = {"FNumber": self.replace_none_str(row.get("F_XYS_SPMC", ""))}
            entity_item["F_XYS_TaxRate"] = self.replace_none_numb(row.get("F_XYS_TaxRate", 0))
            entity_item["F_UNW_CPSMFS"] = self.replace_none_str(row.get("F_UNW_CPSMFS", ""))
            entity_item["F_UNW_SRJC"] = self.replace_none_numb(row.get("F_UNW_SRJC", 0))
            # entity_item["F_XYS_CKKey"] = self.replace_none_str(row.get("F_XYS_CKKey", ""))
            entity_item["F_UNW_FQBM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_FQBM", ""))}
            entity_item["F_UNW_ZGGYXM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_ZGGYXM", ""))}
            entity_item["F_UNW_ZGGYBL"] = self.replace_none_numb(row.get("F_UNW_ZGGYBL", 0))

        # 聚水潭销售退货中间表（金蝶测试251010）
        elif getattr(self, 'table_name', '') == 'kd_sale_return':
            entity_item["FEntryID"] = self.replace_none_numb(row.get("FEntity", 0))
            entity_item["FSkuID"] = self.replace_none_str(row.get("FSkuID", ""))
            entity_item["Fproperties_value"] = self.replace_none_str(row.get("Fproperties_value", ""))
            entity_item["FQTY"] = self.replace_none_numb(row.get("FQTY", 0))
            entity_item["FTAXPRICE"] = self.replace_none_numb(row.get("FTAXPRICE", 0))
            entity_item["Fsale_amount"] = self.replace_none_numb(row.get("Fsale_amount", 0))
            entity_item["FmaterialID"] = {"FNUMBER": self.replace_none_str(row.get("FmaterialID", ""))}
            entity_item["FzkAmount"] = self.replace_none_numb(row.get("FzkAmount", 0))
            entity_item["F_XYS_JSTEntryID"] = self.replace_none_str(row.get("F_XYS_JSTEntryID", ""))
            entity_item["F_XYS_skuname"] = self.replace_none_str(row.get("F_XYS_skuname", ""))
            entity_item["F_XYS_combine_sku_id"] = self.replace_none_str(row.get("F_XYS_combine_sku_id", ""))
            entity_item["F_XYS_is_gift"] = self.replace_none_str(row.get("F_XYS_is_gift","false"))
            entity_item["F_XYS_PTZH"] = {"FNumber": self.replace_none_str(row.get("F_XYS_PTZH", ""))}
            entity_item["F_XYS_XM"] = {"FNumber": self.replace_none_str(row.get("F_XYS_XM", ""))}
            entity_item["F_XYS_GSY"] = {"FNumber": self.replace_none_str(row.get("F_XYS_GSY", ""))}
            entity_item["F_XYS_CPDL"] = {"FNumber": self.replace_none_str(row.get("F_XYS_CPDL", ""))}
            entity_item["F_XYS_PT"] = {"FNumber": self.replace_none_str(row.get("F_XYS_PT", ""))}
            entity_item["F_XYS_BM"] = {"FNUMBER": self.replace_none_str(row.get("F_XYS_BM", ""))}
            entity_item["F_XYS_SPMC"] = {"FNumber": self.replace_none_str(row.get("F_XYS_SPMC", ""))}
            entity_item["F_XYS_TaxRate"] = self.replace_none_numb(row.get("F_XYS_TaxRate", 0))
            entity_item["F_XYS_ioi_id"] = self.replace_none_str(row.get("F_XYS_ioi_id", ""))
            entity_item["F_UNW_CPSMFS"] = self.replace_none_str(row.get("F_UNW_CPSMFS", ""))
            entity_item["F_UNW_SRJC"] = self.replace_none_numb(row.get("F_UNW_SRJC", 0))
            entity_item["F_UNW_FQBM"] = {"FNUMBER": self.replace_none_str(row.get("F_UNW_FQBM", ""))}
            entity_item["F_UNW_ZGGYXM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_ZGGYXM", ""))}
            entity_item["F_UNW_ZGGYBL"] = self.replace_none_numb(row.get("F_UNW_ZGGYBL", 0))

        # 应付单（运费）
        elif getattr(self, 'table_name', '') == 'kd_ship_fee':
            # entity_item["FEntryID"] = self.replace_none_numb(row.get("FEntryID", 0))
            # (明细)费用项目编码#编码
            entity_item["FCOSTID"] = {"FNumber": self.replace_none_str(row.get("FCOSTID", ""))}
            # (明细)费用项目编码#名称
            entity_item["FCOSTIDName"] = self.replace_none_str(row.get("FCOSTIDName", ""))
            # (明细)不含税金额
            entity_item["FNoTaxAmountFor_D"] = self.replace_none_numb(row.get("FNoTaxAmountFor_D", 0))
            # (明细)税额
            entity_item["FTAXAMOUNTFOR_D"] = self.replace_none_numb(row.get("FTAXAMOUNTFOR_D", 0))
            # (明细)价税合计
            entity_item["FALLAMOUNTFOR_D"] = self.replace_none_numb(row.get("FALLAMOUNTFOR_D", 0))
            # (明细)费用承担部门#编码
            entity_item["FCOSTDEPARTMENTID"] = {"FNumber": self.replace_none_str(row.get("FCOSTDEPARTMENTID", ""))}
            # (明细)费用承担部门#名称
            entity_item["FCOSTDEPARTMENTIDName"] = self.replace_none_str(row.get("FCOSTDEPARTMENTIDName", ""))
            # (明细)平台账号#编码
            entity_item["F_UNW_PTZH"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PTZH", ""))}
            # (明细)平台账号#名称
            entity_item["F_UNW_PTZHName"] = self.replace_none_str(row.get("F_UNW_PTZHName", ""))
            # (明细)项目#编码
            entity_item["F_UNW_XM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_XM", ""))}
            # (明细)项目#名称
            entity_item["F_UNW_XMName"] = self.replace_none_str(row.get("F_UNW_XMName", ""))
            # (明细)公私域#编码
            entity_item["F_UNW_GSY"] = {"FNumber": self.replace_none_str(row.get("F_UNW_GSY", ""))}
            # (明细)公私域#名称
            entity_item["F_UNW_GSYName"] = self.replace_none_str(row.get("F_UNW_GSYName", ""))
            # (明细)行业#编码
            entity_item["F_UNW_CPDL"] = {"FNumber": self.replace_none_str(row.get("F_UNW_CPDL", ""))}
            # (明细)行业#名称
            entity_item["F_UNW_CPDLName"] = self.replace_none_str(row.get("F_UNW_CPDLName", ""))
            # (明细)平台#编码
            entity_item["F_UNW_PT"] = {"FNumber": self.replace_none_str(row.get("F_UNW_PT", ""))}
            # (明细)平台#名称
            entity_item["F_UNW_PTName"] = self.replace_none_str(row.get("F_UNW_PTName", ""))
            # (明细)商品名称#编码
            entity_item["F_UNW_SPMC"] = {"FNumber": self.replace_none_str(row.get("F_UNW_SPMC", ""))}
            # (明细)商品名称#名称
            entity_item["F_UNW_SPMCName"] = self.replace_none_str(row.get("F_UNW_SPMCName", ""))
            # (明细)店铺名称
            entity_item["F_UNW_DPMC"] = self.replace_none_str(row.get("F_UNW_DPMC", ""))
            # (明细)快递公司
            entity_item["F_UNW_KDGS"] = self.replace_none_str(row.get("F_UNW_KDGS", ""))
            # (明细)快递单号
            entity_item["F_UNW_KDDH"] = self.replace_none_str(row.get("F_UNW_KDDH", ""))
            # (明细)原始线上订单号
            entity_item["F_UNW_YSXSDDH"] = self.replace_none_str(row.get("F_UNW_YSXSDDH", ""))
            # (明细)仓储方
            entity_item["F_UNW_CCF"] = self.replace_none_str(row.get("F_UNW_CCF", ""))
            # (明细)出库日期
            entity_item["F_UNW_CKRQ"] = self.replace_none_date(str(row.get("F_UNW_CKRQ", "1900-01-01")))
            # (明细)订单费用总计【元】
            entity_item["F_UNW_DDFYZJ"] = self.replace_none_numb(row.get("F_UNW_DDFYZJ", 0))
            # (明细)发起部门#编码
            entity_item["F_UNW_FQBM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_FQBM", ""))}
            # (明细)发起部门#名称
            entity_item["F_UNW_FQBMName"] = self.replace_none_str(row.get("F_UNW_FQBMName", ""))
            # (明细)发货物品
            entity_item["F_UNW_FHWP"] = self.replace_none_str(row.get("F_UNW_FHWP", ""))
            # (明细)是否进项税转回
            entity_item["F_UNW_JXSZH"] = self.replace_none_str(row.get("F_UNW_JXSZH", ""))
            # (明细)转高公域项目编码
            entity_item["F_UNW_ZGGYXM"] = {"FNumber": self.replace_none_str(row.get("F_UNW_ZGGYXM", ""))}
            # (明细)转高公域项目名称
            entity_item["F_UNW_ZGGYXMName"] = self.replace_none_str(row.get("F_UNW_ZGGYXMName", ""))
            # (明细)转高公域比例
            entity_item["F_UNW_ZGGYBL"] = self.replace_none_numb(row.get("F_UNW_ZGGYBL", 0))

    #处理默认值为空值的None
    def replace_none_str(self,obj):
        if obj is None:
            return ""
        else:
            return obj

    # 处理默认值为0的None
    def replace_none_numb(self,obj):
        if obj is None:
            return 0
        else:
            return obj

    # 处理默认值为日期的None
    def replace_none_date(self,obj):
        if obj == 'None':
            return ""
        else:
            return obj


if __name__ == "__main__":
    # 配置日志
    logging.basicConfig(
        level=logging.INFO,
        format='%(asctime)s - %(levelname)s - %(message)s'
    )

    try:
        # 1. 创建数据提取器实例
        extractor = DataExtractor(table_name="")

        # 获取待执行任务
        tasks = extractor.get_pending_tasks()
        if not tasks:
            logging.info("没有待执行任务，程序退出")
            sys.exit(0)

        # 遍历每个任务进行处理
        for task in tasks:
            # 2. 从数据库抽取原始数据
            logging.info(f"开始执行任务ID {task['id']} 的数据抽取...")
            extracted_data = extractor.extract_data(task)

            # 3. 检查抽取结果：无数据则继续下一个任务
            if not extracted_data:
                logging.info(f"任务ID {task['id']} 未抽取到任何数据，继续下一个任务")
                continue
            logging.info(f"任务ID {task['id']} 成功抽取 {len(extracted_data)} 条原始数据")

            # 4. 调用类的分组方法，按单据号分组
            logging.info(f"任务ID {task['id']} 开始按单据号分组数据...")
            bill_groups = extractor.group_data_by_bill_no(extracted_data, task['id'])
            if not bill_groups:
                logging.warning(f"任务ID {task['id']} 数据分组后无有效单据组，继续下一个任务")
                continue

            # 5. 准备TXT文件（单个文件存储所有单据的JSON结果）
            output_dir = "json_to_txt_output"
            os.makedirs(output_dir, exist_ok=True)
            # 生成包含任务ID的唯一文件名
            current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
            txt_file_path = os.path.join(output_dir, f"task_{task['id']}_bills_json_result_{current_time}.txt")

            # 6. 写入TXT文件
            with open(txt_file_path, 'w', encoding='utf-8') as txt_f:
                txt_f.write("=" * 100 + "\n")
                txt_f.write(f"数据JSON转换结果汇总报告（任务ID: {task['id']}）\n")
                txt_f.write(f"报告生成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n")
                txt_f.write(f"来源表名: {extractor.table_name}\n")
                txt_f.write(f"总单据数量: {len(bill_groups)} 个\n")
                txt_f.write(f"总数据明细条数: {len(extracted_data)} 条\n")
                txt_f.write("=" * 100 + "\n\n")

                for idx, (bill_no, bill_data) in enumerate(bill_groups.items(), 1):
                    logging.info(f"任务ID {task['id']} 开始处理第 {idx} 个单据（单据号：{bill_no}），包含 {len(bill_data)} 条明细")

                    # 6.1 转换当前单据数据为金蝶API格式的JSON模型
                    try:
                        json_model = extractor.transform_data(bill_data, bill_no, task['id'])
                    except ValueError as ve:
                        logging.error(f"任务ID {task['id']} - 单据 {bill_no} 转换失败: {str(ve)}")
                        continue
                    # 6.2 格式化JSON
                    formatted_json = json.dumps(json_model, ensure_ascii=False, indent=2)

                    # 6.3 打印到控制台
                    print(f"\n===== 任务ID {task['id']} - 第 {idx} 个单据（单据号：{bill_no}）的JSON转换结果 =====")
                    print(formatted_json)

                    # 6.4 写入TXT文件
                    txt_f.write(f"【第 {idx} 个单据】\n")
                    txt_f.write(f"单据号: {bill_no}\n")
                    txt_f.write(f"明细条数: {len(bill_data)} 条\n")
                    txt_f.write("-" * 80 + "\n")
                    txt_f.write("JSON数据格式（金蝶云API兼容）:\n")
                    txt_f.write(formatted_json + "\n")
                    txt_f.write("\n" + "=" * 100 + "\n\n")

            logging.info(f"任务ID {task['id']} 所有单据处理完成！JSON结果已保存到TXT文件: {os.path.abspath(txt_file_path)}")

        print(f"\n 所有任务执行完成！")

    except Exception as e:
        logging.error(f"程序执行过程中发生错误: {str(e)}", exc_info=True)
        print(f"\n 程序执行失败: {str(e)}")
        sys.exit(1)


